t
Step/Show, Trace, Debug, and Abort

The Stack window and Breakpoints work together with other interpreter features to give you complete control over the interruption, inspection, and editing of your Prograph application*45* during execution. The features of interest are found under the*328* Exec *610*menu. They are the Abort item*9* near the top of the menu, the cluster of*340* Step/Show *342* *343* items including the Breakpoint controls, and the*346* *1105*Trace *630*and*260* *334*Debug *615*items at the bottom of the menu.

Mastering the use of interpreter features that let you control, inspect, and edit your Prograph applications as they run is crucial to increasing the speed with which you learn the Prograph language. Being able to halt and inspect the state of the system as it executes methods can help reduce misconceptions you may have about what happens when your methods are executed. This is especially true since one of the best ways to understand a dataflow language is to freeze the flow and see which data and objects are coming and going.

u
Open the Educational.pgs file in the Tutorial Examples ƒ folder.

u
Run the application and select Iterative Factorial and Recursive Factorial in the Examples menu. In each window, try a few numbers (smaller than 12, unless you go into the Window editor and widen the Factorial Text item so it can display large answers). Enter the same number in each window and compare answers.

When you ran the same number in the Iterative and Recursive Factorial windows earlier and pressed the Compute! button, both windows returned the same answer. But the window titles suggest that each of these windows is getting to its answer through its own method of computation. (The factorial of N, denoted N!, is calculated N! = N * (N-1) * (N-2) …* (N-(N-1)). Example factorials are 3! = 3 * 2 * 1 and 5! = 5 * 4 * 3 * 2 * 1):

To inspect these algorithms more closely, set some*1007* Step/Show and *110* Breakpoint options.

u
Switch context, open the Universal Methods window, and click to select the recursive factorial method icon. Select*1012* Step/Show Level… in the Exec menu*623*. Click the Show Cases option on in the Set Step/Show Level dialog.

u
Click the Set Level button to dismiss the dialog. Open the iterative factorial method case window. Double-click to open the factorial loop local method operation. Select*1015* Step/Show On in the Exec menu and then close the case windows and the Universal Methods window.  
___________________________________________________________
*1008**1009**1010**1011*
NOTE: *1013* Selecting*627* Step/Show On in the Exec menu is the same as selecting*622* Step/Show Level… *341**1012*and clicking both the Single Step and Show Cases options. Selecting*625* *1014*Step/Show Off deselects whichever combination of Step/Show options are set for the currently selected method.
-----------------------------------------------------------

u
Click to activate the Recursive Factorial application window. Enter 5 and press Compute!. You immediately begin the Show Cases animation.

The*982* Show Cases option provides a graphic animation of the execution of a method without requiring you to press Return to execute every operation. The intensive screen updating slows the execution of an application considerably. However, this lets you see what is going on more clearly. 

As you observe the execution windows, you see that recursive factorial momentarily drops to its second case just as it bottoms out and begins to resolve calls to itself. While you can interrupt execution at any time with the Break*105* command  (Command-.), it is often difficult to stop execution exactly where you want. This is especially true if you want to examine execution of a case with just one or two operations. That is where breakpoints*111* come into play. 

In the quickSort example, you set a Breakpoint in advance of execution. But you can set Breakpoints on the fly while your application is running:

u
Activate the Recursive Factorial application window and press Compute! to calculate again the factorial of five.

u
Quickly press Command-. to suspend execution. An execution window, most likely recursive factorial 1:2, will be frontmost. Double-click to open this method’s case window. 

u
Click the right case-selection control in the title bar of the method to open the recursive factorial 2:2 case window.

u
Click to select the output bar of this case and select Breakpoint On in the Exec menu. Command-click to close both case windows.

u
With the suspended-execution window active, press Return to restart the application.

The Show Cases animation continues until the Breakpoint in recursive factorial 2:2 is reached. Execution halts at the breakpoint.

As you might guess, the recursion drops to its second case on the fifth call to recursive factorial. You can activate the*1000* Stack window and inspect the various recursive calls to see how the execution reached its current state. 

___________________________________________________________
NOTE:  *1004*The Stack window monitors the execution of Local methods as well as class and universal methods. Class and universal methods are distinguished in the Stack window by their names appearing in bold text below their associated method icons. Class methods are further distinguished from universal methods by the way in which they are named. The class name of the method is followed by a slash and then the method name.

Local method names appear in plain text in the Stack window. A Local method displayed in the Stack window is always found in the case window of the class or universal method appearing directly above the Local method icon in the Stack window. 

When you suspend an application and activate the Stack window, double-clicking on a Local method opens inside the local, while double-clicking the class or universal method directly above the Local icon in the Stack window shows where the Local operation icon is found within the method containing the local.
-----------------------------------------------------------

In addition, you can back up to rerun a portion of the current execution. The Stack window allows you to force a rollback to any method execution that has an icon above the currently paused execution point.

u
Activate the Stack window. Command-click the first recursive factorial #1 method icon, which is just below the Window/Recursive icon partially obscured by the Stack window title. Like a card dealer in reverse, the execution windows accumulated during the Show Cases animation disappear, along with the method icons appearing below the icon just Command-clicked. This is visible confirmation of the*950* rollback Prograph has just performed.

u
The rollback stops at the first call to recursive factorial 1:2. The rollback has deactivated the Stack window and now leaves the recursive factorial 1:2 execution window active. Double-clicking the root of the input bar opens a Value window that confirms the recursion is ready to restart with its initial input of 5. Close the Value window.

u
Press Return to *946*resume execution. Execution again pauses at the breakpoint.

___________________________________________________________
NOTE:  Execution resumes after a Breakpoint by pressing Return, regardless of which execution window is active. This feature lets you explore the various execution windows leading to the current execution state. These execution windows may already be open if you have been using Step/Show options. Otherwise, double-clicking icons in the Stack window opens these execution windows. Pressing Return at any time lets you resume execution without having to find your way back to the execution window in which the Breakpoint was encountered.
-----------------------------------------------------------

u
Close the Stack window, then press Return to resume execution. 

You can inspect objects and data values flowing along datalinks. You can also edit these values at roots and terminals, and edit a method in the midst of its execution.

u
Activate the Recursive Factorial application window and press Return. 

This time when execution breaks at recursive factorial 2:2, edit the value that exits the output bar:

u
Double-click the terminal of the output bar of recursive factorial 2:2. A Value window opens showing the integer 1 ready to be passed back to the fourth execution of recursive factorial. Type 100 and press Return twice.

The edited data-value is accepted and execution resumes. Instead of computing 120 as a result, this execution returns 12000, reflecting the data value edit causing multiplication by 100 rather than by 1 when the fourth execution of recursive factorial completed execution.
Further, it is even more useful to be able to edit a method in the midst of its execution.

u
Activate the Recursive Factorial application window again and press Return. 

This time when execution breaks at recursive factorial 2:2, edit the case of this method.

u
Double-click anywhere on the background pattern of the recursive factorial 2:2 execution window to open its case window. Add a SysBeep operation to the window and give it an input constant of 30. Close the case window.

u
The interpreter has performed a rollback from the output bar to the input of the SysBeep operation. Press Return twice to execute the beep and again to complete execution of the case.
The ability to edit objects and values passing along datalinks and the ability to edit methods as they execute are powerful features of the Prograph interpreter. These features can prove especially useful as you build increasingly complex programs using Application Builder.

u
Next, activate the Iterative Factorial application window. Enter 5 and press Compute!.
Recall that you selected Step/Show On for the factorial loop local method. Each time this Local method is executed, an execution window opens. In Single Step mode, you must press Return to execute each operation in the stepped method.

u
Repeatedly press Return to step through a couple of iterations of the factorial loop local method.

Step/Show modes and Breakpoints can be very helpful in exploring what your application is doing. But at times all the stop and go can get in the way. Of course you have the option of selecting each method, or local, and individually turning Step/Show settings and Breakpoints Off. Sometimes you may want to see your application running uninterrupted, and then turn the scheduled interruptions back on for additional runtime refinement.

u
While still stepping through the factorial loop executions, select Debug from the Exec menu. Debug should no longer be checked. Press Return and the iterative factorial computation is completed without further interruptions.

u
Activate the Recursive Factorial application window. Backspace to delete the 5 and type 10. Press Return. You hear the system beep, and the answer is displayed in the Recursive Factorial window. 

u
After entering and running a few numbers in each of the Factorial windows, switch context and turn Debug mode back on. Switch context again and run a number. Your Step/Show modes and Breakpoints are working again.

___________________________________________________________
NOTE:  The Debug and*347* Trace *631**1106*items in the Exec menu “toggle” between being off and on. When selected, they flip-flop from their current state to the opposite state. If one of these features is on, a check mark is displayed in front of the item. Select the item—its state turns to off and the check mark is removed. In the current state, Trace is off and unchecked while Debug is on.
-----------------------------------------------------------

Sometimes you want to see every detail of your application execute. To keep from having to set Step/Show mode for all the methods called in the application, the interpreter provides Trace mode. Turning Trace mode on puts the entire application into Single Step and Show Cases mode. The default state of Trace mode is off. Turning Trace mode on in the Educational application lets you examine how the window’s idle method works.

u
Press Command - = to switch context and select Trace in the Exec menu. Activate the Recursive Factorial window. With Trace mode active, the Idle Factorial 1:1 execution window immediately opens.

u
Press Return repeatedly to step through the Idle Factorial method.

Notice that Trace mode steps through everything. When a Local method is encountered, pressing Return opens an execution window on the local. When the local is completed, flow continues in the execution window of the method containing the Local operation.

u
Select Trace, then Debug in the Exec menu to turn Trace and Debug modes off. Press Return, execution continues without being traced. 

There are situations in which you start running your application and simply want to get out of the current execution without completely quitting the application. This situation can arise when you have a blatant error in a method that is executing, and the object or data you expect to process is nowhere to be found. Sure, you could edit a data value or rewrite a method on the fly. But sometimes you just want to retreat and regroup.

The Abort item in the Exec menu lets you stop execution of a method that has been interrupted, whether by your intentional break or by an error. Aborting lets you stop the execution of a method without having to quit the current run of your application. Suppose you want to compute the factorial of 1,000, but choose the wrong tool for the job.

u
Activate the Recursive Factorial window and enter 1000 in the Nonnegative integer field. Press Compute!.

u
Depending on how much RAM is available to the Prograph environment, you are very likely to get the following error-alert dialog:

 

u
Dismiss the dialog. The input bar is *381*flashing*485* in the recursive factorial 1:2 execution window, which opened just prior to the alert dialog. This flashing input bar indicates that the data value anticipated as input did not arrive. Pressing Return only results in more*997* Stack Overflow Error alert dialogs. (If you want to see how far you progressed in this extensive recursion, open the Stack window. Then open the next-to-the-last method icon at the end of the Stack window list. Inspect this execution’s value on the input bar and subtract from 1000. The result is the number of levels deep you went with available RAM.)

u
Using a recursive algorithm for this factorial calculation is clearly not a good idea. Select Abort from the Exec menu. Any open execution windows and the Stack window, if open, close.

u
As if you stepped back in time, the Recursive Factorial window is active with 1000 in its edit field. To confirm that your application has returned to a fully functional state, press Backspace twice to delete a couple of zeros, and then press Return to compute the factorial of 10. The answer 3628800 is promptly returned.

Still wanting to know the factorial of 1,000, you decide an iterative computation would not be subject to the stack-overflowing problem of the recursive solution.

u
Activate the Iterative Factorial window and type 1000 in the text field. Press Compute!.

Even with a fast 68030 or 68040 CPU, this takes some time to calculate, probably 15 to 90 seconds, depending on your CPU. You know it is still working if you cannot pull down a menu and the insertion cursor is frozen on or off. The calculation is complete when the menu bar is again active and the text-field insertion bar again blinks.

You do not, however, see a number on the Factorial is answer line. The factorial of 1,000 is simply too big to display in the space provided, even in scientific notation. But this gives you a chance to refresh your memory on using the*1165* Window editor of Application Builder.

u
Select Enter Editor from the Apple menu. Prograph stops running the Educational application and enters the*308* Window editor.

u
Double-click to open the Text item, which shows as a dotted rectangle. The Text window item dialog opens to reveal the answer in the Text attribute field.

There it is. The factorial of 1,000 is a very large number! Obviously too large a number to put in the space provided for an answer in your application window.

u
You can just dismiss the Text dialog and exit the Window editor to continue on in the tutorial. Or if you want to iteratively calculate a few more large factorials, you can expand the window and the text field where the answers are returned. Then exit the Window editor.

u
After computing a few more factorials and experimenting with the various execution control and online information features of the interpreter, open the Universal Methods window. Click to select the recursive factorial method icon. Select*165* *331*Clear Steps & Breaks *612*from the Exec menu to remove all interruptions from the method-execution process. Repeat the same procedure with the iterative factorial method icon. 

u
Select the Debug menu item to turn it back on, and quit the Educational application.

t
Editor/Interpreter Challenge

This tutorial ends with a simple challenge.

Run some of the applications and sample methods in the Tutorial Examples ƒ and Simple Examples folders. The Learning Prograph folder also has applications and sample methods you can use. Use your new skills to interrupt and control method executions so you can watch their inner workings. And when you see primitives you have not yet encountered, use the online help for explanation. 

Continue to practice using the built-in Prograph help and information facilities and become increasingly comfortable controlling and inspecting the execution of Prograph methods and applications.

With a traditional text-based programming language, the editor is basically a word processor with features to enhance program editing. The interpreter and debugging facilities in such environments usually do little more than provide helpful error messages and then open the editor on the program mistake so that you can correct it.

The editor and interpreter in Prograph are radically more complex than any text-based programming system. While more complex in many ways, good design makes the environment very efficient and powerful in knowledgeable hands.

This tutorial was designed to help you become familiar with Prograph’s help and information facilities and to show you how to control execution under the interpreter. But the survey of features in this chapter is not exhaustive. You can continue to learn about additional features of the editor and interpreter by proceeding with the tutorials in chapters 6, 7, and 8.

t
Check Your Progress

*157*Check the list below. Be sure you are familiar with the basics of the editor/interpreter environment before going on. 

In this chapter you have already learned how to:

4 access different levels of online help by setting Message Preferences in the Options… dialog

4 edit both text and objects using selections from the Edit menu

4 get detailed online descriptions of both Prograph and Macintosh system elements provided through the ˙Info… dialog

4 write your own online documentation for user-defined classes and methods

4 control and examine Prograph classes and methods while an application is running using the Stack window and breakpoints

4 control interruption, inspection, and editing during program execution using Step/Show, Trace, Debug, and Abort features under the Exec menu

Don’t hesitate to take a second look at the activities you covered in this tutorial. The more comfortable you can become with Prograph’s powerful editor and interpreter features, the more quickly you will learn how to make the most of the complete development system. Then, when you are ready to learn more about the basics of the Prograph language, continue on to the next tutorial.